package com.xhh.enjoyapp.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.xhh.enjoyapp.Bean.Community;
import com.xhh.enjoyapp.Bean.Post;
import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.R;
import com.xhh.enjoyapp.adapter.HomeAdapter;
import com.xhh.enjoyapp.constant.CommonConstant;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-14 10:34
 **/
public class FragmentHome extends Fragment {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;
    private TextView tvHome;
    private List<Post> data;
    private HomeAdapter recycleViewAdapter;
    private TextView tvUsername;
    private TextView tvUserWelcome;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_home, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();

        Bmob.initialize(getActivity(), "d3e1bf8031b6974daf3e37a8b57529dd");

        // 初始时刷新
        refresh();

        refreshLayout.setColorSchemeResources(android.R.color.holo_green_light, android.R.color.holo_red_light,android.R.color.holo_blue_light);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 刷新
                refresh();
            }
        });

        // 当前用户user加载
        BmobUser bmobUser = BmobUser.getCurrentUser(User.class);
        String userId = bmobUser.getObjectId();
        BmobQuery<User> bmobQuery = new BmobQuery<>();
        bmobQuery.getObject(userId, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if(e == null){
                    tvUsername.setText(user.getUsername());
                } else {
                    tvUsername.setText("加载失败");
                    tvUserWelcome.setText("");
                }
            }
        });
    }

    private void refresh() {
        BmobQuery<Post> po = new BmobQuery<Post>();
        // 顺序
        po.order("-createdAt");
        po.setLimit(1000);
        po.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                refreshLayout.setRefreshing(false);

                if(e == null){
                    data = list;
                    recycleViewAdapter = new HomeAdapter(getActivity(),data);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView.setAdapter(recycleViewAdapter);

                } else {
                    refreshLayout.setRefreshing(false);
                    Log.e(CommonConstant.FRAGMENT_HOME, "获取数据失败：" + e);
                    Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        recyclerView = getActivity().findViewById(R.id.recycle_view_index);
        refreshLayout = getActivity().findViewById(R.id.swipe_fresh_index);
        tvHome = getActivity().findViewById(R.id.tv_home_index);
        tvUsername = getActivity().findViewById(R.id.tv_home_user);
        tvUserWelcome = getActivity().findViewById(R.id.tv_home_welcome);
    }
}
